/*
 * Copyright (C) 2008-2009 Google Inc.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */

package org.apelikecoder.bulgariankeyboard;

import android.content.SharedPreferences;
import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
import android.content.res.Resources;
import android.os.Bundle;
import android.preference.ListPreference;
import android.preference.PreferenceActivity;
import android.preference.PreferenceManager;

public class BulgarianIMESettings extends PreferenceActivity
                    implements OnSharedPreferenceChangeListener {

    @Override
    protected void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        addPreferencesFromResource(R.xml.prefs);
        setSummary(getString(R.string.prefs_kbd_type),
                R.array.keyboard_type_values,
                R.array.keyboard_type_display_values);
        setSummary(getString(R.string.prefs_vibration_type),
                R.array.vibration_values,
                R.array.vibration_display_values);
        PreferenceManager.getDefaultSharedPreferences(this)
                .registerOnSharedPreferenceChangeListener(this);
    }

    private void setSummary(String prefKey, int keyArrayResId, int valArrayResId) {
        Resources res = getResources();
        ListPreference pref = (ListPreference)findPreference(prefKey);
        String[] keys = res.getStringArray(keyArrayResId);
        String[] vals = res.getStringArray(valArrayResId);
        String defKey = keys[0];
        pref.setDefaultValue(defKey);
        SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(this);
        String key = sp.getString(prefKey, defKey);
        int idx = 0;
        for (int i= 0; i < keys.length; i++)
            if (keys[i].equals(key)) {
                idx = i;
                break;
            }
        pref.setSummary(vals[idx]);
    }

    public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
        if (key.equals(getString(R.string.prefs_kbd_type)))
            setSummary(key, R.array.keyboard_type_values,
                    R.array.keyboard_type_display_values);
        else if (key.equals(getString(R.string.prefs_vibration_type)))
            setSummary(key, R.array.vibration_values,
                    R.array.vibration_display_values);
    }

}
// vim:ts=4:sw=4:expandtab
